package com.wj.controller;


import cn.hutool.json.JSONUtil;
import com.hospital.common.base.Result;
import com.hospital.common.dto.message.module.*;
import com.hospital.common.vo.message.module.FindUserReplayMessageVo;
import com.wj.service.ITMessageService;
import com.wj.service.ITPostContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 消息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-09-28
 */
@RestController
//@RequestMapping("/t-message")
public class TMessageController {

    @Autowired
    private ITMessageService iTMessageService;
    @Autowired
    private SseNotificationController sseController;
    @Autowired
    private ITPostContentService iTPostContentService;
    @PostMapping("/addAdminMessage")
    public Result addAdminMessage(@RequestBody AddAdminMessageDto addAdminMessageDto){
        return Result.success(iTMessageService.addAdminMessage(addAdminMessageDto));
    }
    /**
     * 获取系统通知
     * @param findAdminMessageDto
     * @return
     */
    @PostMapping("/findAdminMessage")
    public Result findAdminMessage(@RequestBody FindAdminMessageDto findAdminMessageDto){
        return Result.success(iTMessageService.findAdminMessage(findAdminMessageDto));
    }
    @PostMapping("/addMessage")
    public Result addMessage(@RequestBody AddMessageDto addMessageDto){

        Long data = iTMessageService.addMessage(addMessageDto);
        if (data>0) {
            // 2. 获取目标用户ID（假设 DTO 里有 receiverUserId 字段）
            Long targetUserId = addMessageDto.getReceiverId(); // 👈 你要确保这个字段存在！


            // 3. 如果目标用户在线，推送 SSE 消息
            if(addMessageDto.getMessageType()==1){
                if (targetUserId != null ) {
                    FindUserReplayMessageVo tMessage = iTMessageService.findOneExamineMessage( data);
                    String contentJson = JSONUtil.toJsonStr(tMessage);
                    sseController.sendMessageToUser(
                            targetUserId,
                            "系统通知",                          // 标题
                            contentJson,   // 内容（从 DTO 取）
                            "admin"                            // 类型
                    );
                }
            }else if(addMessageDto.getMessageType()==2){
                if (targetUserId != null ) {
                    FindUserReplayMessageVo tMessage = iTMessageService.findOneUserReplayMessage( data);
                    String contentJson = JSONUtil.toJsonStr(tMessage);

                    sseController.sendMessageToUser(
                            targetUserId,
                            "用户消息",                          // 标题
                            contentJson,   // 内容（从 DTO 取）
                            "user"                            // 类型
                    );
                }
            }


            return Result.success(data);
        } else {
            return Result.fail("消息发送失败");
        }
    }
    /**
     * 获取审核消息
     * @param findExamineMessageDto
     * @return
     */
    @PostMapping("/findExamineMessage")
    public Result findExamineMessage(@RequestBody FindExamineMessageDto findExamineMessageDto){
        return Result.success(iTMessageService.findExamineMessage(findExamineMessageDto));
    }

    /**
     * 获取用户回复消息
     * @param findUserReplayMessageDto
     * @return
     */
    @PostMapping("/findUserReplayMessage")
    public Result findUserReplayMessage(@RequestBody FindUserReplayMessageDto findUserReplayMessageDto){
        return Result.success(iTMessageService.findUserReplayMessage(findUserReplayMessageDto));
    }
    /**
     * 获取系统通知总数
     */
    @GetMapping("/getAdminMessageCount")
    public Result getAdminMessageCount(){
        return Result.success(iTMessageService.getAdminMessageCount());
    }

    /**
     * 用户已读消息
     */
    @GetMapping("/readMessage")
    public Result readMessage(@RequestParam("messageId") Long metaId){
        return Result.success(iTMessageService.readMessage(metaId));
    }
    /**
     * 用户所有消息已读
     */
    @GetMapping("/readAllMessage")
    public Result readAllMessage(@RequestParam("userId") Long userId,
                                 @RequestParam("messageType") Integer messageType){
        return Result.success(iTMessageService.readAllMessage(userId,messageType));
    }
    /**
     * 获取跳转需要的参数
     */
    @GetMapping("/getJumpParams")
    public Result getJumpParams(@RequestParam("metaId") Long metaId){
        return Result.success(iTMessageService.getJumpParams(metaId));
    }
    @DeleteMapping("/deleteMessage")
    public Result deleteMessage(@RequestParam("messageId") Long messageId){
        return Result.success(iTMessageService.deleteMessage(messageId));
    }

}
